package com.vitalfit.strategy.strategyImpl;

import com.vitalfit.model.dto.HandleDTO;
import com.vitalfit.model.entity.user.Customer;
import com.vitalfit.model.vo.UserPageVO;
import com.vitalfit.repository.UserRepository;
import com.vitalfit.strategy.UserStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class QueryCustomerStrategy implements UserStrategy {

    private final UserRepository userRepository;
    @Override
    @Transactional(readOnly = true)
    public Object execute(HandleDTO handleDTO) {

        int page = (handleDTO.getPage() != null) ? handleDTO.getPage() : 0;
        int size = (handleDTO.getSize() != null) ? handleDTO.getSize() : 10;
        Page<Customer> customerPage = userRepository.findAllCustomers(PageRequest.of(page,size));

        UserPageVO response = new UserPageVO();
        response.setContent(customerPage.getContent());
        response.setTotalPages(customerPage.getTotalPages());
        response.setTotalElements(customerPage.getTotalElements());

        return response;
    }
}
